package com.haonan.springboot_exercise.bean.pojo;

import lombok.Data;

import java.io.Serializable;

/**
 * @author haonan
 * @date 2021/4/14 22:52
 **/

@Data
public class Result implements Serializable {
    // if the custom code is not 20000, it is judged as an error.
    // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
    private int code;
    private String message;
    private Object data;
    public static Result succ(Object data) {
        Result m = new Result();
        m.setCode(20000);
        m.setData(data);
        m.setMessage("操作成功");
        return m;
    }
    public static Result succ(String mess, Object data) {//成功
        Result m = new Result();
        m.setCode(20000);
        m.setData(data);
        m.setMessage(mess);
        return m;
    }
    public static Result disable(String mess) {//禁用
        Result m = new Result();
        m.setCode(30000);
        m.setData(null);
        m.setMessage(mess);
        return m;
    }
    public static Result fail(String mess) {//失败
        Result m = new Result();
        m.setCode(40000);
        m.setData(null);
        m.setMessage(mess);
        return m;
    }
    public static Result fail(String mess, Object data) {
        Result m = new Result();
        m.setCode(40000);
        m.setData(data);
        m.setMessage(mess);
        return m;
    }
    public static Result fail(int code,String mess) {
        Result m = new Result();
        m.setCode(code);
        m.setData(null);
        m.setMessage(mess);
        return m;
    }
    public static Result msgResult2Result(MsgResult msgResult){
        // 当msgResult的code为300，说明操作成功，返回成功Result(不附加data)
        if(msgResult.getCode() == 300){
            return succ(null);
        } else if(msgResult.getCode() == 350){
            return fail("连接错误");
        } else if(msgResult.getCode() == 351){
            return fail("发件人邮箱错误");
        } else if(msgResult.getCode() == 352){
            return fail("收件人邮箱错误，错误邮箱如下:"+msgResult.getData());
        } else if(msgResult.getCode() == 353){
            return fail("错误：主题字数长度超过100");
        } else if(msgResult.getCode() == 400){
            return fail("错误：邮件内容不合规范");
        } else {
            return fail("邮件发送失败，请重试");
        }
    }
}
